<?php
declare (strict_types = 1);

namespace app\admin\model;

use think\Model;

/**
 * @mixin \think\Model
 */
class ChargingStation extends Model
{
    protected $pk = "id";

    /**
     * 获取站点信息信息
     * @param $id
     * @return array|null|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getInfoById($id){
        return $this->where('id',$id)->find();
    }

    /**
     * 站点列表
     * @param $getData
     * @param $page
     * @param $limit
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getList($getData,$page,$limit){
        $where = ['is_delete'=>1];
        $sql = $this->where($where);
        if(isset($getData['title']) && $getData['title']) $sql = $sql->whereLike('title','%'.$getData['title'].'%');
        if(isset($getData['contacts']) && $getData['contacts']) $sql = $sql->whereLike('contacts','%'.$getData['contacts'].'%');
        if(isset($getData['telephone']) && $getData['telephone']) $sql = $sql->whereLike('telephone','%'.$getData['telephone'].'%');
        //街镇
        if(isset($getData['street_id']) && $getData['street_id']){
            $sql = $sql->where('street_id',$getData['street_id']);
        //区域
        }else if(isset($getData['district_id']) && $getData['district_id']){
            $sql = $sql->where('district_id',$getData['district_id']);
        //城市
        }else if(isset($getData['city_id']) && $getData['city_id']){
            $sql = $sql->where('city_id',$getData['city_id']);
        //省份
        }else if(isset($getData['province_id']) && $getData['province_id']){
            $sql = $sql->where('province_id',$getData['province_id']);
        }
        //地址
        if(isset($getData['address']) && $getData['address']) $sql = $sql->whereLike('address','%'.$getData['address'].'%');
        $count = $sql->count();
        $statusType = [0=>'关闭',1=>'开启'];
        $data = $sql->page($page,$limit)->order('id','desc')->select()->each(function ($item)use($statusType){
            $item['status_name'] = $statusType[$item['is_status']];
            $item['picture'] = json_decode($item['picture'],true);
            return $item;
        })->toArray();
        return compact('count','data');
    }

    /**
     *  添加站点
     * @param $addData
     * @return int|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function addData($addData){
        $cityAreaModel = new CityArea();
        $areaName = '';
        if(isset($addData['province_id']) && $addData['province_id']) $areaName = $areaName.($cityAreaModel->getNameById($addData['province_id'])).' ';
        if(isset($addData['city_id']) && $addData['city_id']) $areaName = $areaName.($cityAreaModel->getNameById($addData['city_id'])).' ';
        if(isset($addData['district_id']) && $addData['district_id']) $areaName = $areaName.($cityAreaModel->getNameById($addData['district_id'])).' ';
        if(isset($addData['street_id']) && $addData['street_id']) $areaName = $areaName.($cityAreaModel->getNameById($addData['street_id']));
        return $this->insertGetId([
            'title'=>$addData['title'],
            'contacts'=>$addData['contacts'],
            'telephone'=>$addData['telephone'],
            'picture'=>json_encode($addData['picture']),
            'address'=>$addData['address'],
            'longitude'=>$addData['longitude'],
            'latitude'=>$addData['latitude'],
            'province_id'=>$addData['province_id'],
            'city_id'=>$addData['city_id'],
            'district_id'=>$addData['district_id'],
            'street_id'=>$addData['street_id'],
            'area_name'=>$areaName,
            'is_status'=>$addData['is_status'],
            'remark'=>$addData['remark'],
            'is_delete'=>1,
            'create_time'=>date("Y-m-d H:i:s"),
            'update_time'=>date("Y-m-d H:i:s"),
        ]);
    }

    /**
     * 修改站点
     * @param $saveData
     * @return static
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function editData($saveData){
        $cityAreaModel = new CityArea();
        $areaName = '';
        if(isset($saveData['province_id']) && $saveData['province_id']) $areaName = $areaName.($cityAreaModel->getNameById($saveData['province_id'])).' ';
        if(isset($saveData['city_id']) && $saveData['city_id']) $areaName = $areaName.($cityAreaModel->getNameById($saveData['city_id'])).' ';
        if(isset($saveData['district_id']) && $saveData['district_id']) $areaName = $areaName.($cityAreaModel->getNameById($saveData['district_id'])).' ';
        if(isset($saveData['street_id']) && $saveData['street_id']) $areaName = $areaName.($cityAreaModel->getNameById($saveData['street_id']));
        $updateData = [
            'title'=>$saveData['title'],
            'contacts'=>$saveData['contacts'],
            'telephone'=>$saveData['telephone'],
            'picture'=>json_encode($saveData['picture']),
            'address'=>$saveData['address'],
            'longitude'=>$saveData['longitude'],
            'latitude'=>$saveData['latitude'],
            'province_id'=>$saveData['province_id'],
            'city_id'=>$saveData['city_id'],
            'district_id'=>$saveData['district_id'],
            'street_id'=>$saveData['street_id'],
            'area_name'=>$areaName,
            'is_status'=>$saveData['is_status'],
            'remark'=>$saveData['remark'],
            'update_time'=>date("Y-m-d H:i:s"),
        ];
        return $this->where('id',$saveData['id'])->update($updateData);
    }

    /**
     * 删除站点
     * @param $id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function delData($id){
        $getInfo = $this->getInfoById($id);
        if($getInfo['is_delete'] == 0) return ['code'=>0,'msg'=>'删除成功'];
        $result = $this->where('id',$id)->update(['is_delete'=>0]);
        if($result) return ['code'=>0,'msg'=>'删除成功'];
        return ['code'=>1,'msg'=>'删除失败，请稍后重试'];
    }

    /**
     * 获取地区列表
     * @param $where
     * @param $field
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getAllList($where,$field='*'){
        return $this->where($where)->field($field)->select()->toArray();
    }

}
